package com.hzyl.famousreader.view.widget.core.animation;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;

public class SlideAnim extends HorizonAnimAbs {

  private Rect mSrcRect, mDestRect, mNextSrcRect, mNextDestRect;

  public SlideAnim(View mView, int w, int h, OnPageChangeListener listener) {
    super(mView, w, h, listener);

    mSrcRect = new Rect(0, 0, mViewWidth, mViewHeight);
    mDestRect = new Rect(0, 0, mViewWidth, mViewHeight);
    mNextSrcRect = new Rect(0, 0, mViewWidth, mViewHeight);
    mNextDestRect = new Rect(0, 0, mViewWidth, mViewHeight);
  }

  @Override
  protected void drawAnim(Canvas canvas) {
    int dis = 0;
    switch (mDirection) {
      case NEXT:
        //左半边的剩余区域
        dis = (int) (mScreenWidth - mStartX + mTouchX);
        if (dis > mScreenWidth) {
          dis = mScreenWidth;
        }
        //计算bitmap截取的区域
        mSrcRect.left = mScreenWidth - dis;
        //计算bitmap在canvas显示的区域
        mDestRect.right = dis;
        //计算下一页截取的区域
        mNextSrcRect.right = mScreenWidth - dis;
        //计算下一页在canvas显示的区域
        mNextDestRect.left = dis;

        canvas.drawBitmap(showBitmap, mNextSrcRect, mNextDestRect, null);
        canvas.drawBitmap(noneBitmap, mSrcRect, mDestRect, null);
        break;
      default:
        dis = (int) (mTouchX - mStartX);
        if (dis < 0) {
          dis = 0;
          mStartX = mTouchX;
        }
        mSrcRect.left = mScreenWidth - dis;
        mDestRect.right = dis;

        //计算下一页截取的区域
        mNextSrcRect.right = mScreenWidth - dis;
        //计算下一页在canvas显示的区域
        mNextDestRect.left = dis;

        canvas.drawBitmap(noneBitmap, mNextSrcRect, mNextDestRect, null);
        canvas.drawBitmap(showBitmap, mSrcRect, mDestRect, null);
        break;
    }
  }

  @Override
  protected void startAnim() {
    int dx = 0;
    switch (mDirection) {
      case NEXT:
        if (isCancel) {
          int dis = (int) ((mScreenWidth - mStartX) + mTouchX);
          if (dis > mScreenWidth) {
            dis = mScreenWidth;
          }
          dx = mScreenWidth - dis;
        } else {
          dx = (int) -(mTouchX + (mScreenWidth - mStartX));
        }
        break;
      default:
        if (isCancel) {
          dx = (int) -Math.abs(mTouchX - mStartX);
        } else {
          dx = (int) (mScreenWidth - (mTouchX - mStartX));
        }
        break;
    }
    //滑动速度保持一致
    int duration = (400 * Math.abs(dx)) / mScreenWidth;
    mScroller.startScroll((int) mTouchX, 0, dx, 0, duration);
  }
}
